/*
  SHA-1 Hash Dictionary Attack
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "includes/libm_sha1.h"
#include "includes/libm_aux.h"

#define showTimer 1

#define maxLen 256

int main(int argc, char *argv[])
{
  if (argc != 4)
    {
      printf("SHA-1 Hash MaNGOS Dictionary Attack\n");
      printf("Xkyve Software\n");
      printf("Usage: %s <username> <hash> <dict_file>\n", argv[0]);
      printf("Example: %s admin BC907015034EAD400672DB372C0CF68367E6318C dict/mil-dic.txt\n", argv[0]);
      exit(1);
    }

  if (!sha1_valid(argv[2]))
    {
      printf("Invalid hash specified!\n");
      exit(1);
    }

  FILE *dictFile;

  dictFile = fopen(argv[3], "rt");
  if (dictFile == NULL)
    {
      printf("Unable to open dictionary file \"%s\".\n", argv[3]);
      exit(1);
    }

  str_up(argv[1]); str_up(argv[2]);
  printf("Attempting to break --> %s:%s\n", argv[1], argv[2]);

  int found = 0;
  char buff[maxLen], hash[41];

  #if showTimer
  clock_t start, stop;
  start = clock();
  #endif

  while (!feof(dictFile) && !found)
    {
      readLine(dictFile, buff, maxLen);

      if (!feof(dictFile))
	{
	  str_up(buff);
	  sha1_mangos(argv[1], buff, hash, 0);
	  if (strcmp(argv[2], hash) == 0)
	    {
	      found = 1;
	    }
	}
    }

  #if showTimer
  stop = clock();
  printf("Approx CPU time elapsed: %.3lf seconds.\n", (double) (stop - start) / CLOCKS_PER_SEC);
  #endif

  fclose(dictFile);

  if (found)
    printf("Success! %s:%s\n", argv[1], buff);
  else
    printf("Failed! Password is not within the dictionary file \"%s\"!\n", argv[3]);

  return 0;
}
